package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAccountBinding.persenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAccountBinding.bean.QQbandingBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAccountBinding.model.FirstChangeAccountBindingModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAccountBinding.model.ISFirstChangeAccountBindingModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAccountBinding.view.ISFirstChangeAccountBindingView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

import static android.content.Context.MODE_PRIVATE;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/16
 */

public class FirstChangeAccountBindingPersenter {
    private ISFirstChangeAccountBindingView mISFirstChangeAccountBindingView;
    private ISFirstChangeAccountBindingModel mISFirstChangeAccountBindingModel ;
    public FirstChangeAccountBindingPersenter(ISFirstChangeAccountBindingView iSFirstChangeAccountBindingView) {
        this.mISFirstChangeAccountBindingView = iSFirstChangeAccountBindingView;
        mISFirstChangeAccountBindingModel = new FirstChangeAccountBindingModel();

    }
    //点击 手机号
    public void onClickePhoneNumber(){
        String str = mISFirstChangeAccountBindingView.getPhoneNumber();
        if(TextUtils.isEmpty(str)) {
            mISFirstChangeAccountBindingView.showToastInformation("您没有绑定手机号");
            mISFirstChangeAccountBindingView.toLoginPager();
        }
        mISFirstChangeAccountBindingView.toChangePhoneNumberPager();


    }
    public String getMyUserId(Context mContextdate){
        TakeDateBean bean = mISFirstChangeAccountBindingModel.TakeDate(mContextdate);
        if(bean == null) return "";
        return bean.getUser_id();
    }
    //点击 qq 绑定
    public void onClickeQQBanding(Context mContext){
        TakeDateBean bean = mISFirstChangeAccountBindingModel.TakeDate(mContext);
        if(bean == null) return;
        String QQ = mISFirstChangeAccountBindingModel.TakeQQ(mContext);
       // if (TextUtils.isEmpty(QQ)) return;
      String str =  mISFirstChangeAccountBindingView.getQQBanding();
//       NLogger.e("TAG","============QQ==========####======="+QQ);
       NLogger.e("TAG","==========qq 绑定==QQ===str============="+str);
        if (TextUtils.isEmpty(str)) return;
        if(str.equals("绑定")){
            if(!TextUtils.isEmpty(QQ)){
                banding(bean.getUser_id(),QQ,mContext);
            }else{
                mISFirstChangeAccountBindingView.QQAccountBinding();
            }
        }else if(str.equals("解绑")){
            if(!TextUtils.isEmpty(QQ)){
                banding(bean.getUser_id(),QQ,mContext);
            }else{
                mISFirstChangeAccountBindingView.showToastInformation("绑定失败");
            }
        }
    }


    //点击 微信解绑
    public void onClickeWeChatUnbundling(Context mContext){
        TakeDateBean bean = mISFirstChangeAccountBindingModel.TakeDate(mContext);
        if(bean == null) return;
        String Weixin = mISFirstChangeAccountBindingModel.TakeWeixin(mContext);
        String str =  mISFirstChangeAccountBindingView.getWeChatUnbundling();
       // NLogger.e("TAG","============Weixin==========####======="+Weixin);
       NLogger.e("TAG","=========微信解绑===bean.getUser_id()===str============="+str);
        if (TextUtils.isEmpty(str)) return;
        if(str.equals("绑定")){
            if(!TextUtils.isEmpty(Weixin)){
                bandingWeixin(bean.getUser_id(),Weixin,mContext);
            }else{
                mISFirstChangeAccountBindingView.WeChatAccountBinding();
            }
        }else if(str.equals("解绑")){
            if(!TextUtils.isEmpty(Weixin)){
                bandingWeixin(bean.getUser_id(),Weixin,mContext);
            }else{
                mISFirstChangeAccountBindingView.showToastInformation("绑定失败");
            }

        }
    }

    private void bandingWeixin(String user_id, final String weixin, final Context mContextdate) {
        String url = RBConstants.SERVER_URL+RBConstants.ACCOUNT_BAINDING;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", user_id)
                .addParams("weixin", weixin)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                       // NLogger.e("TAG","============NetworkResponseDate================="+NetworkResponseDate);
                        final QQbandingBean mQQbandingBean =  gson.fromJson(NetworkResponseDate, QQbandingBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(mQQbandingBean.errorCode == 200){
                                    String str =  mISFirstChangeAccountBindingView.getWeChatUnbundling();
                                    SharedPreferences sharedPreferences = mContextdate.getSharedPreferences("My_App_Cardiac", MODE_PRIVATE);
                                    //得到SharedPreferences.Editor对象，并保存数据到该对象中
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    NLogger.e("TAG","============NetworkResponseDate===123===bandingWeixin==========="+str);
                                    if(str.equals("绑定")){
                                        mISFirstChangeAccountBindingView.setWeChatUnbundling(16,"解绑", R.drawable.account_binding_tv_qq_unbinding,0xff2196F3);
                                        mISFirstChangeAccountBindingView.showToastInformation("微信 绑定成功");
                                        editor.putString("my_use_weixin", weixin);
                                    }else{
                                        mISFirstChangeAccountBindingView.setWeChatUnbundling(16,"绑定", R.drawable.account_binding_tv_qq_binding,0xff2196F3);
                                        mISFirstChangeAccountBindingView.showToastInformation("微信 解绑成功"); //解绑成功
                                        editor.putString("my_use_weixin", "");
                                    }
                                    //保存key-value对到文件中
                                    editor.commit();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        //mISFirstChangeAccountBindingView.showToastInformation("请查看网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    public void initDate(Context mContext){
        String phone_number = mISFirstChangeAccountBindingModel.TakePhone(mContext);
        mISFirstChangeAccountBindingView.setPhoneNumber(phone_number);
        String QQ = mISFirstChangeAccountBindingModel.TakeQQ(mContext);

        if(TextUtils.isEmpty(QQ)) {
            mISFirstChangeAccountBindingView.setQQBanding(16,"绑定", R.drawable.account_binding_tv_qq_binding,0xff2196F3);
        }else{
            mISFirstChangeAccountBindingView.setQQBanding(16,"解绑", R.drawable.account_binding_tv_qq_unbinding,0xff666666);
        }
        String weixin = mISFirstChangeAccountBindingModel.TakeWeixin(mContext);
        if(TextUtils.isEmpty(weixin)) {
            mISFirstChangeAccountBindingView.setWeChatUnbundling(16,"绑定", R.drawable.account_binding_tv_qq_binding,0xff2196F3);
        }else{
            mISFirstChangeAccountBindingView.setWeChatUnbundling(16,"解绑", R.drawable.account_binding_tv_qq_unbinding,0xff666666);
        }


    }


    private void banding(String user_id, final String qq, final Context mContextdate) {
        String url = RBConstants.SERVER_URL+RBConstants.ACCOUNT_BAINDING;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", user_id)
                .addParams("qq", qq)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();

                        final QQbandingBean mQQbandingBean =  gson.fromJson(NetworkResponseDate, QQbandingBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(mQQbandingBean.errorCode == 200){
                                    String str =  mISFirstChangeAccountBindingView.getQQBanding();
                                    SharedPreferences sharedPreferences = mContextdate.getSharedPreferences("My_App_Cardiac", MODE_PRIVATE);
                                    //得到SharedPreferences.Editor对象，并保存数据到该对象中
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    NLogger.e("TAG","============NetworkResponseDate===123===banding==========="+str);
                                    if(str.equals("绑定")){
                                        mISFirstChangeAccountBindingView.setQQBanding(16,"解绑", R.drawable.account_binding_tv_qq_unbinding,0xff666666);
                                        mISFirstChangeAccountBindingView.showToastInformation("qq 绑定成功"); //解绑成功
                                        editor.putString("my_use_qq",qq);
                                    }else{
                                        mISFirstChangeAccountBindingView.setQQBanding(16,"绑定", R.drawable.account_binding_tv_qq_binding,0xff2196F3);
                                        mISFirstChangeAccountBindingView.showToastInformation("qq 解绑成功");
                                        editor.putString("my_use_qq","");
                                    }
                                    //保存key-value对到文件中
                                    editor.commit();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                     //   mISFirstChangeAccountBindingView.showToastInformation("请查看网络");
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

}
